package tcp

import (
	"gse/transport"
	"net"
)

type TCPTransportConfig struct {
	*TCPConnectionConfig
}

type TCPTransport struct {
	*net.TCPListener
	cfg *TCPTransportConfig
}

func NewDefaultTCPTransportConfig() *TCPTransportConfig {
	return &TCPTransportConfig{
		TCPConnectionConfig: &TCPConnectionConfig{
			NoDelay: true,
		},
	}
}

func NewTcpTransport(listener *net.TCPListener, config *TCPTransportConfig) *TCPTransport {
	if config.TCPConnectionConfig == nil {
		config.TCPConnectionConfig = &TCPConnectionConfig{}
	}
	return &TCPTransport{
		TCPListener: listener,
		cfg:         config,
	}
}
func (l *TCPTransport) Accept() (transport.Connection, error) {
	tcpConn, err := l.AcceptTCP()
	if err != nil {
		return nil, err
	}
	return NewTcpConnection(tcpConn, l.cfg.TCPConnectionConfig), nil
}

func (l *TCPTransport) Shutdown() {
	l.Close()
}
